import {computed, ref,watch} from 'vue'
import { defineStore } from 'pinia'
import {addcardApi,getcardApi,delcardApi,mergercardApi,gaicardApi} from "@/apis/card.js";

export const useCardstore = defineStore('card', () => {
    const cardlist = ref([])
    const addcard = async (obj,token)=>{
        if(token){
            const {skuId,count} = obj
            const p = await addcardApi({skuId,count})
            getcard()
        }else{
            const item = cardlist.value.find((ele)=>{ return ele.skuId === obj.skuId})
            if(item){
                item.count += obj.count
            }else{
                cardlist.value.push(obj)
            }
        }

    }
    const clearcard = ()=>{
        cardlist.value = []
    }
    // const watchcard = ()=>{
    //     cardlist.value.forEach(ele=>{
    //         watch(ele,async (newele)=>{
    //             if(cardlist.value.length > 0){
    //                 const p = await gaicardApi({
    //                     id:newele.skuId,
    //                     selected:newele.selected,
    //                     count:newele.count
    //                 })
    //
    //             }
    //         })
    //     })
    // }
    const getcard = async ()=>{
        const p = await getcardApi()
        cardlist.value = p.data.result
    }
    const delcard = async (id,token) => {
        if(token){
            const p = await delcardApi([id])
            getcard()
        }
        const item = cardlist.value.findIndex((ele)=> {
            return ele.skuId === id
        })
        cardlist.value.splice(item,1)

    }
    const merge =async ()=>{
        if(cardlist.value.length>0){
            let arr = cardlist.value.map(ele=>{
                return {
                    skuId:ele.skuId,
                    selected:ele.selected,
                    count:ele.count
                }
            })
            const p = await mergercardApi(arr)
        }
        getcard()
        }

    const sum = computed(()=>{
        let sum = 0
        for (let i = 0;i<cardlist.value.length;i++){
            sum+=cardlist.value[i].count
        }
        return sum
    })
    const selectedsum = computed(()=>{
        let sum = 0
        for (let i = 0;i<cardlist.value.length;i++){
            if(cardlist.value[i].selected)
            sum+=cardlist.value[i].count
        }
        return sum
    })
    const price = computed(()=>{
        let price = 0
        for (let i = 0;i<cardlist.value.length;i++){
            price+=cardlist.value[i].count*cardlist.value[i].price
        }
        return price.toFixed(2)
    })
    const selectedprice = computed(()=>{
        let price = 0
        for (let i = 0;i<cardlist.value.length;i++){
            if(cardlist.value[i].selected)
            price+=cardlist.value[i].count*cardlist.value[i].price
        }
        return price.toFixed(2)
    })
    const selected = computed({
        get(){
            return cardlist.value.length === 0 ? false : cardlist.value.every(ele => ele.selected)
        },
        set(value){
            cardlist.value.forEach(ele => {
                ele.selected = value
            })

        }
    })
    return {
        cardlist,addcard,delcard,clearcard,merge,getcard,price,sum,selected,selectedsum,selectedprice
    }
},{ persist: true})
